package com.shanyin.framework.api.server.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.shanyin.framework.common.base.ResponseData;

/***
 ** @category 请用一句话来描述其用途...
 ** @author qing.yunhui
 ** @email: 280672161@qq.com
 ** @createTime: 2018年5月25日下午10:05:09
 **/
@RestController
@RequestMapping(LoginController.ACTION_PATH)
public class LoginController {
	
	final static String ACTION_PATH="/api/user";
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private DiscoveryClient discoveryClient;
	
	//使用EurekaClient获取服务注册信息
	/*@Autowired
	private EurekaClient discoveryClient;
	public String serviceUrl() {
	    InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);
	    return instance.getHomePageUrl();
	}*/
	
	//discoveryClient获取服务列表中，应用名为sc-eureka-first-provider一个服务注册信息
	public String serviceUrl() {
		List<ServiceInstance> list = discoveryClient.getInstances("member-provider");
		if (list != null && list.size() > 0) {
			logger.info("ServiceInstance={}.",new Object[] {JSONObject.toJSONString(list)});
			return String.valueOf(list.get(0).getUri());
		}
		return null;
	}
	
	/**
     * <p>登陆[支持手机号|邮箱]</p>
     * @param phone 手机号
     * @param password 密码
     * @param isPhone true:手机号登陆、false:邮箱登陆
     * */
	@GetMapping("/login")
	public ResponseData login(String phone,String password,boolean isPhone) {
		String providerServiceUrl = serviceUrl();
		Map<String, Object> map = new HashMap<String,Object>();
	    map.put("phone", phone);
	    map.put("password", password);
	    map.put("isPhone", isPhone);
	    logger.info("=======>输入的参数有:{}.",new Object[] {JSONObject.toJSONString(map)});
		return this.restTemplate.getForObject(providerServiceUrl + "member/sys/user/login?phone={phone}&password={password}&isPhone={isPhone}",ResponseData.class,map);
	}
	
	/**
     * <p>注册</p>
     * @param phone 注册手机号
     * @param password 密码
     * @param registerChannel 注册渠道
     * @param invitationCode  邀请码
     * */
	@GetMapping("/registered")
	public ResponseData registered(String phone,String password,String registerChannel,String invitationCode) {
		String providerServiceUrl = serviceUrl();
		Map<String, Object> map = new HashMap<String,Object>();
	    map.put("phone", phone);
	    map.put("password", password);
	    map.put("registerChannel", registerChannel);
	    map.put("invitationCode", invitationCode);
	    logger.info("=======>输入的参数有:{}.",new Object[] {JSONObject.toJSONString(map)});
		return this.restTemplate.getForObject(providerServiceUrl + "member/sys/user/registered?phone={phone}&password={password}&registerChannel={registerChannel}&invitationCode=${invitationCode}",ResponseData.class,map);
	}

}
